/*  iImager (network disk imaging via iscsi)
	Copyright (C) 2009-2010 Yao Wei <njustyw@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "lib_imaging.h"
#include "device_handle.h"
#include "Q_HDObject.h"

HDObject::HDObject(int rIdx, int lIdx, const QString& ip, QObject* parent) : 
			QObject(parent), rackIndex(rIdx), locationIndex(lIdx), ip(ip)
{
	state = HD_UnValid;

    devname = "";
    vendorID = "";
    productID = "";
    sn = "";

	total_bytes = 0;
	processed_bytes = 0;
	bytes_per_sector = 0;
	mean_speed = 0;
}

bool HDObject::determineDisk(const QString &dev_name)
{
	uint8_t media_type = 0;
	struct device_handle device;
	char error_msg[1024];

	if(device_handle_initialize(&device) < 0)
        return false;

	if(device_handle_open_input(&device, dev_name.toUtf8().data(), error_msg) < 0)
        return false;

	if(device_handle_get_media_size(&device, &total_bytes) < 0)
        goto clean;

    if(device_handle_determine_media_information(&device) < 0)
		goto clean;

    if(device_handle_get_media_type(&device, &media_type) < 0)
		goto clean;

    if(device_handle_get_bytes_per_sector(&device, &bytes_per_sector) < 0)
		goto clean;

	devname = dev_name;
	vendorID = QString::fromLatin1((char*)device.vendor);
	productID = QString::fromLatin1((char*)device.model);
	sn = QString::fromLatin1((char*)device.serial_number);

	device_handle_close(&device, error_msg);

	return true;

clean:
	device_handle_close(&device, error_msg);
	return false;
}

bool HDObject::registerDisk(const QString &devName)
{
	if(devName == devname && state != HD_UnValid)	
		return true;

	if(devName.isEmpty())	
		return false;
	
	if(determineDisk(devName) == false)
		return false;

	setState(HD_Normal);

	DEBUG3_MSG("DiskInfo: " + devname + vendorID + " " + productID + " " + sn);

    return true;
}

bool HDObject::removeDisk()
{
	if(state == HD_UnValid)	return true;

	DEBUG3_MSG("Remove disk " + devname);

	devname = "";
	sn = "";
	devname = "";
	productID = "";
	vendorID = "";
	total_bytes = 0;
	processed_bytes = 0;
	bytes_per_sector = 0;
	mean_speed = 0;

	setState(HD_UnValid);

	return true;
}

void HDObject::setState(HDState _state)
{
	if(state == _state)
		return;

	DEBUG3_MSG(QString("(%1 --> %2) : %3").arg(state).arg(_state).arg(devname));

	state = _state;
	emit stateChanged(state);
}

bool HDObject::isValid()const
{ 
	if(state == HD_UnValid)
		return false;
	return true;
}

bool HDObject::isCopying()const
{ 
	return state == HD_Imaging;
}
